package com.hss01248.dialog.adapter;

import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.RecycleItemProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/22 0022.
 */
public  abstract   class SuperRcvAdapter extends RecycleItemProvider implements Refreshable,ILifeCycle {


    private List datas;
    private Context context;
    boolean isListViewFling;

    public boolean isListViewFling() {
        return isListViewFling;
    }

    public void setListViewFling(boolean listViewFling) {
        isListViewFling = listViewFling;
    }



    public SuperRcvAdapter( Context context){

        this.datas = new ArrayList();
        this.context = context;


    }

    /**
     * 工具方法
     * @param layoutRes
     * @return
     */
    public Component inflate( int layoutRes){
        LayoutScatter scatter = LayoutScatter.getInstance(context);
        return scatter.parse(layoutRes, null, false);
    }

    @Override
    public int getCount() {
        if (datas == null )
            return 0;
        return datas.size();
    }



    @Override
    public Object getItem(int position) {
        if (datas == null)
            return null;
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        if (datas == null){
            return 0;
        }
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        SuperRcvHolder holder = null;
        if (convertView == null){
            holder = generateCoustomViewHolder(getItemComponentType(position));
            convertView = holder.rootView;
            convertView.setTag(holder);
        }else {
            holder = (SuperRcvHolder) convertView.getTag();
        }
        holder.assignDatasAndEvents(context, datas.get(position));
        return convertView;
    }
    protected abstract SuperRcvHolder generateCoustomViewHolder(int viewType);
    @Override
    public void refresh(List newData) {
        if (newData == null){
            datas.clear();
            notifyDataChanged();
            return;
        }
        if (datas == null){
            datas = newData;
            notifyDataChanged();
        }else {
            datas.clear();
            datas.addAll(newData);
            notifyDataChanged();
        }
    }

    @Override
    public void addAll(List newData) {
        if (newData == null){
            return;
        }
        if (datas == null){
            datas = newData;
            notifyDataChanged();
        }else {
            datas.addAll(newData);
            notifyDataChanged();
        }
    }

    @Override
    public void clear() {
        if (datas != null){
            datas.clear();
            notifyDataChanged();
        }
    }

    @Override
    public void delete(int position) {
        if (datas != null && position < getCacheSize()){
            datas.remove(position);
            notifyDataSetItemRemoved(position);
        }
    }

    @Override
    public void add(Object object) {
        if (object != null){
            datas.add(object);
            notifyDataSetItemInserted(datas.size() -1);
        }

    }

    public List getListData(){
        return datas;
    }

    @Override
    public void onDestory() {

    }
}
